﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreFrame.Business;
using MarchEstate.Manager.BusinesEntity;
using CoreFrame.Core.Orm;

namespace MarchEstate.Manager.Dao.Costform
{
    public class CostformDao : AbstractDao
    {
        public AbstractMeter GetMeter(string _meterId)
        {
            return BindEntity<AbstractMeter>.CreateInstanceDAL(oleDb, WorkId).GetModel((object)_meterId);
        }

        public List<AbstractMeter> GetMeterList(int _meterType, int _meterType2)
        {
            //根据条件查询用表 且用表不再本次用表表单中
            //且必须改业主已入伙
            string strWhere = @" meterType={0} and meterType2={1} 
                                and (SELECT count(*) FROM WY_MeterHead AS a LEFT OUTER JOIN WY_MeterOrder AS b ON a.MeterHeadId = b.MeterHeadId WHERE (a.CompleteFlag = 0) and b.MeterId=WY_Meter.MeterId)=0
                                and (select JoinFlag from(
                                        select RoomId as srccId, JoinFlag from 
		                                        WY_RoomOwnerRelation 
                                                union all
                                                select ShopId as srccId,JoinFlag from WY_Shop where DeleteFlag=0
                                                union all
                                                select CarPortId as srccId,JoinFlag from WY_Carport
                                                union all
                                                select CustomerId as srccId,JoinFlag from WY_Customer where DeleteFlag=0
                                        ) A where srccId=WY_Meter.SrccId
                                     )=1";
            strWhere = string.Format(strWhere, _meterType, _meterType2);
            return BindEntity<AbstractMeter>.CreateInstanceDAL(oleDb, WorkId).GetListArray(strWhere);
        }

        public List<AbstractMeter> GetMeterList(int _meterType, int _meterType2,PageInfo page)
        {
            //且必须改业主已入伙
            string strWhere = @" meterType={0} and meterType2={1} and (SELECT count(*) FROM WY_MeterHead AS a LEFT OUTER JOIN WY_MeterOrder AS b ON a.MeterHeadId = b.MeterHeadId WHERE (a.CompleteFlag = 0) and b.MeterId=WY_Meter.MeterId)=0
            and (select JoinFlag from(
                                        select RoomId as srccId, JoinFlag from 
		                                        WY_RoomOwnerRelation 
                                                union all
                                                select ShopId as srccId,JoinFlag from WY_Shop where DeleteFlag=0
                                                union all
                                                select CarPortId as srccId,JoinFlag from WY_Carport
                                                union all
                                                select CustomerId as srccId,JoinFlag from WY_Customer where DeleteFlag=0
                                        ) A where srccId=WY_Meter.SrccId
                                     )=1";
            strWhere = string.Format(strWhere, _meterType, _meterType2);
            return BindEntity<AbstractMeter>.CreateInstanceDAL(oleDb, WorkId).GetListArray(strWhere,page);
        }

        public List<AbstractMeter> GetMeterList(int _meterType, int _meterType2, string searchKey, PageInfo page)
        {
            //且必须改业主已入伙
            string strWhere = @" meterType={0} and meterType2={1} and (OwnerName like '%{2}%' or SrccName like '%{2}%') and (SELECT count(*) FROM WY_MeterHead AS a LEFT OUTER JOIN WY_MeterOrder AS b ON a.MeterHeadId = b.MeterHeadId WHERE (a.CompleteFlag = 0) and b.MeterId=WY_Meter.MeterId)=0
            and (select JoinFlag from(
                                        select RoomId as srccId, JoinFlag from 
		                                        WY_RoomOwnerRelation 
                                                union all
                                                select ShopId as srccId,JoinFlag from WY_Shop where DeleteFlag=0
                                                union all
                                                select CarPortId as srccId,JoinFlag from WY_Carport
                                                union all
                                                select CustomerId as srccId,JoinFlag from WY_Customer where DeleteFlag=0
                                        ) A where srccId=WY_Meter.SrccId
                                     )=1";
            strWhere = string.Format(strWhere, _meterType, _meterType2, searchKey);
            return BindEntity<AbstractMeter>.CreateInstanceDAL(oleDb, WorkId).GetListArray(strWhere, page);
        }

        public List<AbstractMeter> GetAllMeterList(int _meterType, int _meterType2, PageInfo page)
        {
            string strWhere = " meterType={0} and meterType2={1} ";
            strWhere = string.Format(strWhere, _meterType, _meterType2);
            return BindEntity<AbstractMeter>.CreateInstanceDAL(oleDb, WorkId).GetListArray(strWhere, page);
        }

        public List<AbstractMeter> GetAllMeterList(string _srccId, PageInfo page)
        {
            string strWhere = " SrccId='{0}'";
            strWhere = string.Format(strWhere,_srccId);
            return BindEntity<AbstractMeter>.CreateInstanceDAL(oleDb, WorkId).GetListArray(strWhere, page);
        }

        

        public bool AddMeter(AbstractMeter meter)
        {
            BindEntity<AbstractMeter>.CreateInstanceDAL(oleDb, WorkId).Add(meter);
            return true;
        }

        public bool DeleteMeter(string _meterid)
        {
            return BindEntity<AbstractMeter>.CreateInstanceDAL(oleDb, WorkId).Delete((object)_meterid) > 0;
        }

        public bool UpdateMeterInitNum(string _meterId, decimal _initNum)
        {
            string strsql = @"update WY_Meter set InitMeterNum={0} where MeterId='{1}'";
            strsql = string.Format(strsql, _initNum, _meterId);
            return oleDb.DoCommand(strsql) > 0;
        }

        public bool AddMaterHeadform(AbstractMeterHead head)
        {
            BindEntity<AbstractMeterHead>.CreateInstanceDAL(oleDb, WorkId).Add(head);
            return true;
        }

        public List<AbstractMeterHead> GetMeterHeadList(int meterType, int meterType2, int _completeFlag)
        {
            return BindEntity<AbstractMeterHead>.CreateInstanceDAL(oleDb, WorkId).GetListArray("meterType=" + meterType + " and meterType2=" + meterType2 + " and CompleteFlag=" + _completeFlag + " order by MeterDate");
        }

        public List<AbstractMeterHead> GetMeterHeadList(int meterType, int meterType2, int _completeFlag, DateTime bdate, DateTime edate, CoreFrame.Core.Orm.PageInfo page)
        {
            string strWhere = "(MeterDate >= '"+bdate.ToString()+"') AND (MeterDate <= '"+edate.ToString()+"') and meterType=" + meterType + " and meterType2=" + meterType2 + " and CompleteFlag=" + _completeFlag + " order by MeterDate";
            return BindEntity<AbstractMeterHead>.CreateInstanceDAL(oleDb, WorkId).GetListArray(strWhere,page);
        }

        public bool DeleteMeterHead(string _meterHeadId)
        {
            string strsql = @"delete from WY_MeterHead where MeterHeadId='"+_meterHeadId+"'";
            oleDb.DoCommand(strsql);
            strsql = @"delete from WY_MeterOrder where MeterHeadId='" + _meterHeadId + "'";
            oleDb.DoCommand(strsql);
            return true;
        }


        public List<AbstractMeterHead> GetMeterHeadList(int _completeFlag)
        {
            return BindEntity<AbstractMeterHead>.CreateInstanceDAL(oleDb, WorkId).GetListArray(" CompleteFlag=" + _completeFlag + " order by MeterDate");
        }

        public bool AddMaterOrder(AbstractMeterOrder order)
        {
            BindEntity<AbstractMeterOrder>.CreateInstanceDAL(oleDb, WorkId).Add(order);
            return true;
        }

        public bool UpdateMaterOrder(AbstractMeterOrder order)
        {
            BindEntity<AbstractMeterOrder>.CreateInstanceDAL(oleDb, WorkId).Update(order);
            return true;
        }

        public bool DeleteMeterOrder(string _meterOrderId)
        {
            BindEntity<AbstractMeterOrder>.CreateInstanceDAL(oleDb, WorkId).Delete((object)_meterOrderId);
            return true;
        }

        public List<AbstractMeterOrder> GetMeterOrderList(string _meterHeadId)
        {
            return BindEntity<AbstractMeterOrder>.CreateInstanceDAL(oleDb, WorkId).GetListArray("MeterHeadId='" + _meterHeadId + "' ");
        }

        public List<AbstractMeterOrder> GetMeterOrderList(string _meterHeadId, PageInfo page)
        {
            return BindEntity<AbstractMeterOrder>.CreateInstanceDAL(oleDb, WorkId).GetListArray("MeterHeadId='" + _meterHeadId + "' ", page);
        }

        public AbstractMeterOrder GetMeterOrder(string _meterorderId)
        {
            return BindEntity<AbstractMeterOrder>.CreateInstanceDAL(oleDb, WorkId).GetModel((object)_meterorderId);
        }

        public decimal GetMeterPrice(string _meterId)
        {
            string strsql = @"SELECT  Price FROM         WY_ServiceItem
                                WHERE     (Code =(SELECT     ItemCode  FROM  WY_Meter
                                                            WHERE      (MeterId = '{0}')))";
            strsql = string.Format(strsql, _meterId);
            return Convert.ToDecimal(oleDb.GetDataResult(strsql));
        }
        /// <summary>
        /// 检查表单记录是否全部录入本次数量
        /// </summary>
        /// <returns></returns>
        public bool CheckrecordMeterorder(string _meterHeadId)
        {
            string strsql = @"SELECT     COUNT(*)  
                                FROM         WY_MeterOrder
                                WHERE     (MeterHeadId = '{0}') AND (ThisNum = 0)";
            strsql = string.Format(strsql, _meterHeadId);
            return Convert.ToInt32(oleDb.GetDataResult(strsql)) > 0;
        }

        public bool UpdateCompleteFlag(string _meterHeadId)
        {
            string strsql = @"update WY_MeterHead set CompleteFlag=1 where MeterHeadId='{0}'";
            strsql = string.Format(strsql, _meterHeadId);
            return oleDb.DoCommand(strsql) > 0;
        }
    }
}
